﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class ProductController : Controller
    {
        DbQuery _dbQuery = new DbQuery();
        //
        // GET: /Product/
        //[OutputCache(Duration=60, VaryByParam=null)]
        public ActionResult Index(int? cpage, int? ppage)
        {
            try
            {
                int categoryPerPage = 10;
                ViewData["CategoriesPerPage"] = categoryPerPage;
                var categories = _dbQuery.getCategoryByID(0, cpage, categoryPerPage, ppage, 10);
                Response.Write(categories.CategoryID);
                Response.Write(categories.CategoryName);
                return View(categories);
            }
            catch (System.ArgumentException)
            {
                int categoryPerPage = 10;
                ViewData["CategoriesPerPage"] = categoryPerPage;
                var categories = _dbQuery.getCategoryByID(0, 1, categoryPerPage, ppage, 10);
                return View(categories);
            }
            catch
            {
                throw;
            }
        }

        [ChildActionOnly]
        public ActionResult Category()
        {
            return PartialView();
        }

        public ActionResult Browse(int? id, int? cpage, int? ppage)
        {
            try
            {
                int categoryPerPage = 10;
                ViewData["CategoriesPerPage"] = categoryPerPage;
                var categories = _dbQuery.getCategoryByID(id, cpage, categoryPerPage, ppage, 10);
                return View("Index", categories);
            }
            catch (System.ArgumentException)
            {
                int categoryPerPage = 10;
                ViewData["CategoriesPerPage"] = categoryPerPage;
                var categories = _dbQuery.getCategoryByID(0, 1, categoryPerPage, ppage, 10);
                return View("Index", categories);
            }
            catch
            {
                throw;
            }
        }

        public ActionResult Details(int id)
        {
            return View("Browse");
        }

        public ActionResult Review()
        {
            return View();
        }

        public ActionResult Test(int? id, int? page)
        {
            var categories = _dbQuery.getSubCategoryList(0, page);
            return View(categories);
        }

        [HttpGet]
        public JsonResult EfficientPaging(int? page)
        {
            var data = _dbQuery.getSubCategoryList(0, page);
            var grid = new WebGrid(data);
            var htmlString = grid.GetHtml(tableStyle: "webGrid",
                    headerStyle: "header",
                    alternatingRowStyle: "alt",
                    htmlAttributes: new { id = "DataTable" });
            return Json(new
            {
                Data = htmlString.ToHtmlString(),
                Count = Math.Ceiling((float)_dbQuery.countSubCategory(0) / 10)
            }, JsonRequestBehavior.AllowGet);
        }
    }
}
